﻿using GN.Pay.TradeChannel.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.Configure
{
    [Serializable]
    public abstract class ATradeChannelConfigure<TConfig> : ITradeChannelConfigure where TConfig : ITradeChannelConfigureInfo, new()
    {
        protected ATradeChannelConfigure(IChannel channel)
        {
            this.Channel = channel;
        }

        /// <summary>
        /// 获取通道
        /// </summary>
        public IChannel Channel { get; private set; }

        ITradeChannelConfigureInfo ITradeChannelConfigure.ConfigureInfo
        {
            get
            {
                return this.ConfigureInfo;
            }
            set
            {
                if (value is TConfig)
                {
                    this.ConfigureInfo = (TConfig)value;
                }
            }
        }

        void ITradeChannelConfigure.SaveCheck(ITradeChannelConfigureInfo configureInfo)
        {
            if (configureInfo != null && configureInfo is TConfig)
            {
                this.SaveCheck((TConfig)configureInfo);
            }
        }

        /// <summary>
        /// 检查非null和
        /// </summary>
        /// <param name="value"></param>
        /// <param name="name"></param>
        protected T CheckNotNull<T>(T value, string name)
        {
            if (value == null)
            {
                if (name == null)
                {
                    name = "";
                }
                throw this.Channel.ThrowChannelValidationException(name + " 值不能为 null");
            }
            return value;
        }

        /// <summary>
        /// 检查非null和非空白属性
        /// </summary>
        /// <param name="value"></param>
        /// <param name="name"></param>
        protected void CheckNotNullOrWhite(string value, string name)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                if (name == null)
                {
                    name = "";
                }
                throw this.Channel.ThrowChannelValidationException(name + " 值不能为 null 或空白值。");
            }
        }

        protected abstract void SaveCheck(TConfig configureInfo);


        public TConfig ConfigureInfo { get; set; }

        public abstract string GetOrderQueryErrorMessage(string code);

        public abstract string GetPayErrorMessage(string code);

        public abstract string GetRefundErrorMessage(string code);

        /// <summary>
        /// 获取编码
        /// </summary>
        public virtual Encoding Encoding
        {
            get
            {
                return Encoding.UTF8;
            }
        }

        /// <summary>
        /// 获取通知控制器名称
        /// </summary>
        protected abstract string NotifyControllerName { get; }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string NotifyRootUrl()
        {
            if (string.IsNullOrWhiteSpace(Consts.NotifyRootUrl))
            {
                throw new TradeChannelException(TradeChannelErrorCode.ChannelValidataion, "", "未配置 NotifyRootUrl");
            }
            return Consts.NotifyRootUrl;
        }

        /// <summary>
        /// 获取付款通知url
        /// </summary>
        public virtual string PayNotifyUrl
        {
            get
            {
                return string.Format("{0}/Notify/{1}/OrderPay", NotifyRootUrl(), NotifyControllerName);
            }
        }

        /// <summary>
        /// 获取付款返回Url
        /// </summary>
        public virtual string PayReturnUrl
        {
            get
            {
                return string.Format("{0}/Notify/{1}/OrderPayReturn", NotifyRootUrl(), NotifyControllerName);
            }
        }

        /// <summary>
        /// 获取付款 Wap 返回Url
        /// </summary>
        public virtual string PayWapReturnUrl
        {
            get
            {
                return string.Format("{0}/Notify/{1}/OrderPayWapReturn", NotifyRootUrl(), NotifyControllerName);
            }
        }

        /// <summary>
        /// 获取退款通知url
        /// </summary>
        public virtual string RefundNotifyUrl
        {
            get
            {
                return string.Format("{0}/Notify/{1}/OrderRefund", NotifyRootUrl(), NotifyControllerName);
            }
        }


    }
}
